package org.lee.cr;

/**
 * Account2
 *
 * @author dralee
 * @date 2022-01-13 10:52:20
 **/
public class Account2 {
    private int balance;

    public Account2(int balance) {
        this.balance = balance;
    }

    public synchronized void transfer(Account2 target, int amount) { // synchronized(this)锁定的只是当前对象
        if (this.balance > amount) {
            this.balance -= amount;
            target.balance += amount;  // 对方未锁住
        }
    }

    @Override
    public String toString() {
        return "Account2{" +
                "balance=" + balance +
                '}';
    }
}
